package com.yhcnet.blog.controller;

import com.yhcnet.blog.pojo.User;
import com.yhcnet.blog.reponse.Resp;
import com.yhcnet.blog.reponse.UserBody;
import com.yhcnet.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    /**
     * 用户登录，返回OK和UUID
     * @param userBody
     * @return
     */
    @RequestMapping("/login")
    public Resp login(@RequestBody UserBody userBody){
        if (userBody == null || userBody.getEmail() == null || "".equals(userBody.getEmail()) || userBody.getPassword() == null || "".equals(userBody.getPassword())) return Resp.loginFail();//空参数
        String uuid = userService.login(userBody);
        if(uuid != null){
            return Resp.OK(uuid);
        }
        return Resp.loginFail();
    }

    /**
     * 用户注册，不会重复注册，返回code和msg
     * @param user
     * @return
     */
    @RequestMapping("/regist")
    public Resp regist(@RequestBody User user){
        if(user == null || user.getEmail() == null || user.getPassword() == null || "".equals(user.getEmail()) || "".equals(user.getPassword()) || "".equals(user.getUsername())) return Resp.loginFail();
        if(userService.regist(user)){
            return Resp.OK();
        }
        return Resp.loginFail();
    }

    /**
     * 返回所有用户的部分非私密信息
     * @return
     */
    @RequestMapping("/getUser")
    public Resp getUser(){
        return Resp.OK(userService.getUser());
    }

    @RequestMapping("/getUserByEmail")
    public Resp getUserByEmail(String email){
        return Resp.OK(userService.getUserByEmail(email));
    }

    @RequestMapping("/test")
    public Resp test(){
        return Resp.OK();
    }


}
